package com.news.gpb.controller;

import com.news.gpb.entity.ToEmail;
import com.news.gpb.entity.User;
import com.news.gpb.util.VerCodeGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin("http://localhost:8080")
@RestController
//@RequestMapping("email")
public class EmailController {

    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;

    @RequestMapping("sendEmail")
    public String commonEmail(@RequestBody User user)
    {
        //        创建邮件消息
        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from);

        message.setTo(user.getUser_email());

        message.setSubject("您本次的验证码是");

        String verCode = VerCodeGenerateUtil.generateVerCode();
        message.setText("您好:\n"
                + "\n本次请求的邮件验证码为:" + verCode + ",本验证码 5 分钟内有效，请及时输入。（请勿泄露此验证码）\n"
                + "\n如非本人操作，请忽略该邮件。\n(这是一封通过自动发送的邮件，请不要直接回复）");

        mailSender.send(message);
        System.out.println(verCode);
        return verCode;
    }
}
